package easycloud.ecs.service;

import java.io.File;
import java.io.IOException;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import easycloud.ecs.App;
import easycloud.ecs.api.IProvider;
import easycloud.ecs.api.IPutResult;
import easycloud.ecs.api.ProviderException;
import easycloud.ecs.api.model.IStorageObject;
import easycloud.ecs.api.model.IStorageObjectMetadata;
import easycloud.ecs.service.FileAlternationListener.ALTERNATION;

class CompareLocalThread implements Runnable {

	private static final Log logger = LogFactory
			.getLog(CompareLocalThread.class);

	private IProvider provider;

	private File file;

	private String objectName;

	private ALTERNATION alternation;

	public CompareLocalThread(IProvider provider, File file, String objectName,
			ALTERNATION alternation) {
		this.provider = provider;
		this.file = file;
		this.objectName = objectName;
		this.alternation = alternation;
	}

	@Override
	public void run() {
		try {
			if ((alternation == ALTERNATION.CHANGE)
					| (alternation == ALTERNATION.CREATE)) {
				IStorageObjectMetadata metadata = provider.head(objectName);
				if (metadata == null) {
					IPutResult result = provider.putFile(file, objectName);
					ETagService.getInstance().updateETag(provider, file,
							result.getETag());
					App.getInstance()
							.getMainFrame()
							.addConsoleMsg(
									"Local -> " + provider.getName() + " ："
											+ file.getName());
				} else {
					if (ETagService.getInstance().isETagDiff(provider, file,
							metadata.getETag())
							&& new Date(file.lastModified()).after(metadata
									.getLastModified())) {
						IPutResult result = provider.putFile(file, objectName);
						ETagService.getInstance().updateETag(provider, file,
								result.getETag());
						App.getInstance()
								.getMainFrame()
								.addConsoleMsg(
										"Local -> " + provider.getName() + " ："
												+ file.getName());
					} else if (ETagService.getInstance().isETagDiff(provider,
							file, metadata.getETag())
							&& new Date(file.lastModified()).before(metadata
									.getLastModified())) {
						IStorageObject downloadObject = provider
								.getFile(objectName);
						FileUtils.copyInputStreamToFile(
								downloadObject.getObjectContent(), file);
						file.setLastModified(metadata.getLastModified()
								.getTime());
						ETagService.getInstance().updateETag(provider, file,
								downloadObject.getETag());
						App.getInstance()
								.getMainFrame()
								.addConsoleMsg(
										provider.getName() + " -> Local ："
												+ file.getName());
					}
				}
			} else if (alternation == ALTERNATION.DELETE) {
				provider.delete(objectName);
				ETagService.getInstance().updateETag(provider, file, null);
				App.getInstance()
						.getMainFrame()
						.addConsoleMsg(
								"Delete " + provider.getName() + " ："
										+ file.getName());
			}
		} catch (IOException e) {
			CompareLocalThread.logger.warn(
					"Download file failed: " + file.getAbsolutePath(), e);
		} catch (ProviderException e) {
			CompareLocalThread.logger.warn(
					"Compare local file failed: " + file.getAbsolutePath(), e);
		}

	}
}